package chooongg.base.utils

import android.annotation.SuppressLint
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.StringRes
import chooongg.base.R
import chooongg.base.manager.app
import com.airbnb.lottie.LottieAnimationView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

object TipUtils {

    private var toast: Toast? = null
    private var gravity: Int = Gravity.CENTER
    private var xOffset: Int = 0
    private var yOffset: Int = 0
    private var colorBG: Int = -1
    private var colorBGRes: Int = -1
    private var colorText: Int = app.resColor(R.color.textVital)

    @JvmOverloads
    fun setGravity(gravity: Int, xOffset: Int = 0, yOffset: Int = 0): TipUtils {
        this.gravity = gravity
        this.xOffset = xOffset
        this.yOffset = yOffset
        return this
    }

    fun setBackgroundColor(@ColorInt colorBG: Int): TipUtils {
        this.colorBG = colorBG
        return this
    }

    fun setBackgroundRes(@ColorRes colorBGRes: Int): TipUtils {
        this.colorBGRes = colorBGRes
        return this
    }

    fun setTextColor(@ColorInt colorText: Int): TipUtils {
        this.colorText = colorText
        return this
    }

    /**
     * 取消Tip显示
     */
    fun cancel() {
        toast?.cancel()
        toast = null
    }

    @SuppressLint("InflateParams")
    fun showSuccess(message: CharSequence, duration: Int) {
        cancel()
        GlobalScope.launch(Dispatchers.Main) {
            toast = Toast(app)
            val view = LayoutInflater.from(app).inflate(R.layout.view_tip, null)
            toast?.view = view
            toast?.duration = duration
            toast?.setGravity(gravity, xOffset, yOffset)
            view.findViewById<LottieAnimationView>(R.id.view_lottie).apply {
                setAnimation(R.raw.lottie_tip)
                setMinAndMaxFrame(239, 400)
                speed = 2f
                playAnimation()
            }
            view.findViewById<TextView>(R.id.tv_message).text = message
            toast?.show()
        }
    }

    @SuppressLint("InflateParams")
    fun showFail(message: CharSequence, duration: Int) {
        cancel()
        GlobalScope.launch(Dispatchers.Main) {
            toast = Toast(app)
            val view = LayoutInflater.from(app).inflate(R.layout.view_tip, null)
            toast?.view = view
            toast?.duration = duration
            toast?.setGravity(gravity, xOffset, yOffset)
            view.findViewById<LottieAnimationView>(R.id.view_lottie).apply {
                setAnimation(R.raw.lottie_tip)
                setMinAndMaxFrame(649, 810)
                speed = 2f
                playAnimation()
            }
            view.findViewById<TextView>(R.id.tv_message).text = message
            toast?.show()
        }
    }

    @SuppressLint("InflateParams")
    fun showWarn(message: CharSequence, duration: Int) {
        cancel()
        GlobalScope.launch(Dispatchers.Main) {
            toast = Toast(app)
            val view = LayoutInflater.from(app).inflate(R.layout.view_tip, null)
            toast?.view = view
            toast?.duration = duration
            toast?.setGravity(gravity, xOffset, yOffset)
            view.findViewById<LottieAnimationView>(R.id.view_lottie).apply {
                setAnimation(R.raw.lottie_tip_info)
                speed = 1.5f
                playAnimation()
            }
            view.findViewById<TextView>(R.id.tv_message).text = message
            toast?.show()
        }
    }
}

fun tipSuccess(message: CharSequence, duration: Int = Toast.LENGTH_LONG) {
    TipUtils.showSuccess(message, duration)
}

fun tipFail(message: CharSequence, duration: Int = Toast.LENGTH_LONG) {
    TipUtils.showFail(message, duration)
}

fun tipWarn(message: CharSequence, duration: Int = Toast.LENGTH_LONG) {
    TipUtils.showWarn(message, duration)
}

fun tipSuccess(@StringRes resId: Int, duration: Int = Toast.LENGTH_LONG) {
    TipUtils.showSuccess(app.resString(resId), duration)
}

fun tipFail(@StringRes resId: Int, duration: Int = Toast.LENGTH_LONG) {
    TipUtils.showFail(app.resString(resId), duration)
}

fun tipWarn(@StringRes resId: Int, duration: Int = Toast.LENGTH_LONG) {
    TipUtils.showWarn(app.resString(resId), duration)
}

fun tipCancel() {
    TipUtils.cancel()
}